package com.example.mylogin;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class Adaper extends BaseAdapter {
    private List<DataBean> datalist;
    private Context context;
    private OnItemClick onItemClick;

    public Adaper(Context context,List<DataBean> datalist) {
        this.context=context;
        this.datalist = datalist;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public int getCount() {
        return datalist!=null?datalist.size():0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
      Holder holder;
      if(view==null){
          holder=new Holder();
          view= LayoutInflater.from(context).inflate(R.layout.list_item_layout,null);
            holder.coname=view.findViewById(R.id.text_test);
            holder.conid=view.findViewById(R.id.imge_icon);
            view.setTag(holder);
      }else{
         holder=(Holder) view.getTag();
      }
      holder.coname.setText(datalist.get(i).getUsername());
      holder.conid.setImageResource(datalist.get(i).getUserid());
      holder.conid.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(onItemClick!=null){
                  onItemClick.OnClick(i);
              }
//            //  Toast.makeText(context,"点击了"+datalist.get(i).getUsername()+"的头像",Toast.LENGTH_SHORT).show();
          }
      });
//        TextView coname=view.findViewById(R.id.text_test);
//        ImageView conid=view.findViewById(R.id.imge_icon);
//        String name=datalist.get(i).getUsername();
//        int id=datalist.get(i).getUserid();
//        coname.setText(name);
//        conid.setImageResource(id);
        return view;
    }
    interface OnItemClick{
        void OnClick(int i);
    }
    class Holder{
        private ImageView conid;
        private  TextView coname;

    }
}
